//
//  UIView+add.m
//  ButtonTest
//
//  Created by guosm on 2021/4/27.
//

#import "UIView+add.h"

@implementation UIView (add)

NSString const *UIButton_addEdgeInsets = @"UIButton_addEdgeInsets";
@dynamic addEdgeInsets;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    /// 无法响应
    if (!self.userInteractionEnabled) {
        return NO;
    }
    /// 没有添加 使用父类方法
    if (UIEdgeInsetsEqualToEdgeInsets(self.addEdgeInsets, UIEdgeInsetsZero)) {
        return [super pointInside:point withEvent:event];
    }
    /// 有添加响应区域
    CGRect enableRect = CGRectMake(self.bounds.origin.x-self.addEdgeInsets.left, self.bounds.origin.y-self.addEdgeInsets.top, self.bounds.size.width+self.addEdgeInsets.right+self.addEdgeInsets.left, self.bounds.size.height+self.addEdgeInsets.bottom+self.addEdgeInsets.top);
    return CGRectContainsPoint(enableRect, point);
}

#pragma mark --Getter
-(UIEdgeInsets )addEdgeInsets {
    NSValue *value = objc_getAssociatedObject(self, &UIButton_addEdgeInsets);
    if (value) {
        return value.UIEdgeInsetsValue;
    }
    return UIEdgeInsetsZero;
}
#pragma mark --Setter
- (void)setAddEdgeInsets:(UIEdgeInsets)addEdgeInsets{
    NSValue *value = [NSValue valueWithUIEdgeInsets:addEdgeInsets];
    objc_setAssociatedObject(self, &UIButton_addEdgeInsets, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
